package com.qms.repast.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.qms.repast.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/9/26 0026
 * @describe: 通用WebView界面（无交互）
 */
public class WebActivity extends AppCompatActivity {
	@BindView(R.id.iv_back)
	ImageView ivBack;
	@BindView(R.id.tv_title)
	TextView tvTitle;
	@BindView(R.id.wb_view)
	WebView wbView;
	private String title;
	private String loadUrl;
	private WebSettings webSettings;
	/**
	 * 是否沉浸式
	 */
	protected boolean isTrans;
	@Override
	protected void onCreate (@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
			finish();
			return;
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			initStatusBar(isTrans);
		}
		setContentView(R.layout.activity_web);
		ButterKnife.bind(this);
		loadUrl = getIntent().getStringExtra("loadUrl");
		title = getIntent().getStringExtra("title");
		initView();
	}

	/**
	 * 初始化布局
	 */
	private void initView () {
		if (!TextUtils.isEmpty(title)) {
			tvTitle.setText(title);
		}
		webSettings = wbView.getSettings();
		webSettings.setDomStorageEnabled(true);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setLoadWithOverviewMode(true);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			webSettings.setMixedContentMode(
					WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
		}
		wbView.loadUrl(loadUrl);
		wbView.setFocusable(false);
		wbView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed(); // 接受网站证书
			}

			@Override
			public boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request) {
				view.loadUrl(loadUrl);
				return true;
			}

		});

	}
	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public void initStatusBar (boolean isTransparent) {
		Window window = getWindow();
		window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		if (isTransparent) {
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
																| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
		} else {
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
																| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		}
		isTrans = isTransparent;
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(Color.WHITE);
	}
	@Override
	public boolean onKeyDown (int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && wbView.canGoBack()) {
			wbView.goBack();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 销毁WebView
	 */
	@Override
	protected void onDestroy () {
		if (wbView != null) {
			wbView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
			wbView.clearHistory();
			((ViewGroup) wbView.getParent()).removeView(wbView);
			wbView.destroy();
			wbView = null;
		}
		super.onDestroy();
	}

	@OnClick(R.id.iv_back)
	public void onViewClicked () {
		finish();
	}
}
